<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\UserService;
use Illuminate\Http\Request;

class LoginController extends BaseController
{
    //首页

    public function __construct()
    {
    }

    //登录
    public function dologin(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $role_type = $request->post('role_type', 0); //1用户，2代理商，3管理员
        $uname = $request->post('username', '');
        $upwd = $request->post('password', '');
        if (!in_array($role_type, [1,2,3])) {
            return $this->toJson(-2, '账号类型必须的');
        }
        if (mb_strlen($uname) < 2 || mb_strlen($uname) > 255) {
            return $this->toJson(-2, '账号名必须2到255位字符之间');
        }
        if (!preg_match('/[0-9a-zA-Z*#&_]{6,20}/', $upwd)) {
            return $this->toJson(-2, '密码格式错误');
        }
        $res = (new UserService())->dologin($role_type, $uname, $upwd);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //用户信息
    public function getInfo(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $res = (new UserService())->getInfo($token);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}